// pages/infomation/childCpns/infoItems.js

let app = getApp()
import {
  previewImg,
  previewImgList
} from '../../../behavior/previewImg/previewImg.js'
import {
  request,
  requestHasToken
} from '../../../service/network'

Component({
  behaviors: [previewImg, previewImgList], //  继承behavior.js里面的properties，data，methods
  properties: {
    info: {
      type: Object,
      value: {}
    },
    admin: {
      type: Boolean,
      value: false
    },
    state: {
      type: String,
      value: '已审核'
    },
    normalAdmin: {
      type: Boolean,
      value: false
    },
    notLogin: {
      type: Boolean,
      value: false
    }
  },
  data: {
    baseurl: App.config.baseApi,
    currentOpenid: wx.getStorageInfoSync('currentOpenid'),
    timer: 0,
    second: 200,
    info: {},
  },
  methods: {
    handleMore(e) {
      let _this = this
      // console.log(e);
      // console.log(e.currentTarget.dataset.id);
      if (this.properties.state == '已审核') {
        wx.showActionSheet({
          itemList: ['详情','复制','修改', '删除'],
          success(res) {
            if (res.tapIndex == 0) {
              wx.navigateTo({
                url: '/pages/person/person?id=' + e.currentTarget.dataset.id,
              })
            } else if (res.tapIndex == 1) {
              _this.copyFunc()
            } else if (res.tapIndex == 2) {
              wx.navigateTo({
                url: '/pages/infoCurd/infoCurd?id=' + e.currentTarget.dataset.id,
              })
            } else if (res.tapIndex == 3) {
              const datainfo = {
                id: e.currentTarget.dataset.id,
                name: _this.data.info.nickname
              }
              _this.triggerEvent("infoDel", datainfo, {})
            }
          },
          fail(res) {
            console.log(res.errMsg)
          }
        })
      } else if (this.properties.state == '未审核') {
        wx.showActionSheet({
          itemList: ['审核通过', '审核未通过'],
          success(res) {
            if (res.tapIndex == 0) {
              requestHasToken({
                url: 'api/ap/information/updateState',
                data:{
                  id: e.currentTarget.dataset.id,
                  state: '已审核'
                }
              }).then((res) => {
                if (res.data.code == 0) {
                  wx.showToast({
                    title: '审核通过!',
                    icon: 'success',
                    duration: 1000
                  })
                  const datainfo = {
                    id: e.currentTarget.dataset.id,
                    name: _this.data.info.nickname
                  }
                  _this.triggerEvent("updateInfosFresh", datainfo, {})
                }
              })
            } else if (res.tapIndex == 1) {
              requestHasToken({
                url: 'api/ap/information/updateState',
                data:{
                  id: e.currentTarget.dataset.id,
                  state: '未通过'
                }
              }).then((res) => {
                if (res.data.code == 0) {
                  wx.showToast({
                    title: '审核未通过!',
                    icon: 'success',
                    duration: 1000
                  })
                  const datainfo = {
                    id: e.currentTarget.dataset.id,
                    name: _this.data.info.nickname
                  }
                  _this.triggerEvent("updateInfosFresh", datainfo, {})
                }
              })
            }
          },
          fail(res) {
            console.log(res.errMsg)
          }
        })
      }

    },
    handleZan(e) {
      if (this.loginPage()) {
        clearTimeout(this.data.timer)
        //防抖
        let timer = setTimeout(() => {
          // console.log(this.data.info.zanId,this.data.info.zanCount);
          if (this.data.info.zanId) {
            requestHasToken({
              url: 'api/ap/informationZan/remove',
              data: {
                id: this.data.info.zanId
              },
            }).then((res) => {
              // console.log(res);
              
              if (res.data.code == 0) {
                this.setData({
                  'info.zanId': '',
                  'info.zanCount': parseInt(this.data.info.zanCount)-1
                })
                // console.log(this.data.info.zanCount,11);
              }
            })
          } else {
            requestHasToken({
              url: 'api/ap/informationZan/save',
              data: {
                infoid: e.currentTarget.dataset.id
              },
            }).then((res) => {
              if (res.data.code == 0) {
                this.setData({
                  'info.zanId': res.data.id,
                  'info.zanCount': parseInt(this.data.info.zanCount)+1
                })
                // console.log(this.data.info.zanCount,22);
              }
            })
          }

        }, this.data.second)
        this.setData({
          timer
        })
      }
    },
    loginPage() {
      if (this.properties.notLogin) {
        wx.redirectTo({
          url: '/pages/login/login?url=/pages/infomation/infomation'
        })
        return false
      } else {
        return true
      }
    },
    copyFunc() {
      let _data = this.data.info
      var data = `昵称： ${_data.nickname} ${_data.gender=='1'?'男':'女'} ${_data.marriage}
年龄：${_data.age }
身高/体重：${_data.height}/${_data.weight}
学历/职业：${_data.education}/${_data.occupation}
联系方式：${_data.phone}
房车：${_data.house}/${_data.car}
自身情况：${_data.comment}
定居何处：${_data.address}
爱好：${_data.hobby}
择偶要求：${_data.requirement}
赏金：${_data.reward}`
          wx.setClipboardData({
            data: data,
            success: function (res) {
            }
          })
    },
  },
  lifetimes: {
    created() {
      // console.log('组件被创建出来时');
    },
    attached() {
      // console.log('组件被添加到页面');

    },
    ready() {
      // console.log('组件被渲染出来');

      this.setData({
        info: this.properties.info
      })
    },
    moved() {
      // console.log('组件被移动到另一个节点');

    },
    detached() {
      // console.log('组件被移除掉');

    },
  }
})